<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Location extends MY_Controller {

    protected $lang;

    public function __construct() {

        parent::__construct();


        $this->data['menu_config'] = $this->menu_config_6;

        $country_session = $this->session->userdata('user');
        $this->data['user_session'] = $country_session;
        if (empty($country_session)) {
            redirect(admin_url('authenticate/login'));
        }
        $this->load_lang('admincp/location');
        $this->lang = $this->lang->language;
        $this->data['lang'] = $this->lang;
        $this->permission();
        $this->load->model('country_model', 'country');
        $this->load->model('zone_model', 'zone');
    }

    public function zone() {
        $this->data['countries'] = $this->country->getCountries();
        $this->load_view('location/zone', 'admincp');
    }

    public function country() {
        $this->load_view('location/country', 'admincp');
    }

    public function zone_lists($start = 0) {
        $posts = $this->input->post();

        $dataWhere = array();
        if (!empty($posts['search']))
            $dataWhere['search'] = $posts['search'];
        if (!empty($posts['country']))
            $dataWhere['country_id'] = $posts['country'];
        $limit = $this->configs['record_per_page'];
//       Begin pagination
        $this->load->library("pagination");
        $config = array();
        $config["total_rows"] = $this->zone->totalZones($dataWhere);
        $config["base_url"] = admin_url('location/zone_lists');
        $config["per_page"] = $limit;
        $page = $start;
        $config["uri_segment"] = 4;
        $config['num_links'] = 2;

        $config['first_link'] = $this->lang['first_link'];
        $config['first_tag_open'] = '<div class="nav-button">';
        $config['first_tag_close'] = '</div>';
        $config['last_link'] = $this->lang['last_link'];
        $config['last_tag_open'] = '<div class="nav-button">';
        $config['last_tag_close'] = '</div>';
        $config['cur_tag_open'] = "<div class='nav-button'><div class='nav-page nav-page-selected'>";
        $config['cur_tag_close'] = '</div></div>';
        $config['num_tag_open'] = "<div class='nav-button'><div class='nav-page'>";
        $config['num_tag_close'] = '</div></div>';
        $config['prev_tag_open'] = "<div class='nav-button'>";
        $config['prev_link'] = $this->lang['prev_link'];
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = $this->lang['next_link'];
        $config['next_tag_open'] = "<div class='nav-button'>";
        $config['next_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        $json["links"] = $this->pagination->create_links();
//       End pagination

        $this->data['zones'] = $this->zone->getZones($dataWhere, $limit, $start);
        $json['zones'] = $this->load->view('admincp/location/zone_lists', $this->data, true);
        echo json_encode($json);
    }

    public function country_lists($start = 0) {
        $posts = $this->input->post();

        $dataWhere = array();
        if (!empty($posts['search']))
            $dataWhere['search'] = $posts['search'];

        $limit = $this->configs['record_per_page'];
//       Begin pagination
        $this->load->library("pagination");
        $config = array();
        $config["total_rows"] = $this->country->totalCountries($dataWhere);
        $config["base_url"] = admin_url('location/country_lists');
        $config["per_page"] = $limit;
        $page = $start;
        $config["uri_segment"] = 4;
        $config['num_links'] = 2;

        $config['first_link'] = $this->lang['first_link'];
        $config['first_tag_open'] = '<div class="nav-button">';
        $config['first_tag_close'] = '</div>';
        $config['last_link'] = $this->lang['last_link'];
        $config['last_tag_open'] = '<div class="nav-button">';
        $config['last_tag_close'] = '</div>';
        $config['cur_tag_open'] = "<div class='nav-button'><div class='nav-page nav-page-selected'>";
        $config['cur_tag_close'] = '</div></div>';
        $config['num_tag_open'] = "<div class='nav-button'><div class='nav-page'>";
        $config['num_tag_close'] = '</div></div>';
        $config['prev_tag_open'] = "<div class='nav-button'>";
        $config['prev_link'] = $this->lang['prev_link'];
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = $this->lang['next_link'];
        $config['next_tag_open'] = "<div class='nav-button'>";
        $config['next_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        $json["links"] = $this->pagination->create_links();
//       End pagination

        $this->data['countries'] = $this->country->getCountries($dataWhere, $limit, $start);
        $json['countries'] = $this->load->view('admincp/location/country_lists', $this->data, true);
        echo json_encode($json);
    }

    public function delete_country($id = 0) {
        $this->country->delete($id);
    }

    public function delete_zone($id = 0) {
        $this->zone->delete($id);
    }

    public function country_form($id = 0) {
        $country = $this->country->getCountryById($id);
        if ($country) {
            $this->data['country'] = $country;
        }

        $posts = $this->input->post();

        if ($posts) {

            foreach ($posts as $key => $post) {
                $country->$key = $post;
            }
            $this->data['country'] = $country;

            if ($this->country_validate($country)) {
                if (empty($country->country_id)) {
                    $country_id = $this->country->insert($posts);
                    redirect(admin_url('location/country'));
                    $this->session->set_flashdata('success', $this->lang['add_success']);
                } else {
                    $this->country->update($country->country_id, $posts);
                    redirect(admin_url('location/country'));
                    $this->session->set_flashdata('success', $this->lang['edit_success']);
                }
            }
        }
        $this->html_country_form($country);
        $this->load_view('location/country_form', 'admincp');
    }

    public function zone_form($id = 0) {
        $zone = $this->zone->getZoneById($id);
        if ($zone) {
            $this->data['zone'] = $zone;
        }

        $posts = $this->input->post();

        if ($posts) {

            foreach ($posts as $key => $post) {
                $zone->$key = $post;
            }
            $this->data['zone'] = $zone;

            if ($this->country_validate($zone)) {
                if (empty($zone->zone_id)) {
                    $zone_id = $this->zone->insert($posts);
                    redirect(admin_url('location/zone'));
                    $this->session->set_flashdata('success', $this->lang['add_success']);
                } else {
                    $this->zone->update($zone->zone_id, $posts);
                    redirect(admin_url('location/zone'));
                    $this->session->set_flashdata('success', $this->lang['edit_success']);
                }
            }
        }
        $this->html_zone_form($zone);
        $this->load_view('location/zone_form', 'admincp');
    }

    private function country_validate($country) {
        $posts = $this->input->post();

        $flag = true;

        if (!$this->hasPermission('modify')) {
            $this->data['error'][] = $this->lang['error_permission_modify'];
            $flag = false;
        }

        return $flag;
    }

    public function active($id = 0) {
        $this->user->update($id, array('status' => 1));
    }

    public function deactive($id = 0) {
        $this->user->update($id, array('status' => 0));
    }

    private function html_zone_form($zone) {
        $countries = $this->country->getCountries();
        foreach ($countries as $country) {
            $countryList[$country->country_id] = $country->name;
        }
        $this->data['form'] = array(
            array(
                'type' => 'input',
                'label' => $this->lang['entry_zname'],
                'value' => array(
                    'name' => 'name',
                    'id' => 'name',
                    'class' => 'st-forminput',
                    'value' => !empty($zone->name) ? $zone->name : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_code'],
                'value' => array(
                    'name' => 'code',
                    'id' => 'code',
                    'class' => 'st-forminput',
                    'value' => !empty($zone->code) ? $zone->code : ''
                )
            ),
            array(
                'type' => 'dropdown',
                'label' => $this->lang['entry_country'],
                'value' => array(
                    array(
                        'name' => 'country_id',
                        'options' => $countryList,
                        'selected' => !empty($zone->country_id) ? $zone->country_id : '',
                        'extra' => 'class="uniform"'
                    ),
                )
            ),
            array(
                'type' => 'checkbox',
                'label' => $this->lang['entry_top'],
                'value' => array(
                    array(
                        'name' => 'top',
                        'id' => 'top',
                        'checked' => !empty($zone->top) ? TRUE : FALSE,
                        'value' => '1',
                        'label' => ''
                    )
                )
            ),
            array(
                'type' => 'dropdown',
                'label' => $this->lang['entry_status'],
                'value' => array(
                    array(
                        'name' => 'status',
                        'options' => array('1' => 'active', '0' => 'de-active'),
                        'selected' => !empty($country->status) ? $country->status : '',
                        'extra' => 'class="uniform"'
                    ),
                )
            )
        );

        $this->data['input_submit'] = array(
            'value' => $this->lang['btn_save'],
            'class' => 'button-blue'
        );
    }

    private function html_country_form($country) {

        $this->data['form'] = array(
            array(
                'type' => 'input',
                'label' => $this->lang['entry_name'],
                'value' => array(
                    'name' => 'name',
                    'id' => 'name',
                    'class' => 'st-forminput',
                    'value' => !empty($country->name) ? $country->name : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_iso_code_2'],
                'value' => array(
                    'name' => 'iso_code_2',
                    'id' => 'iso_code_2',
                    'class' => 'st-forminput',
                    'value' => !empty($country->iso_code_2) ? $country->iso_code_2 : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_iso_code_3'],
                'value' => array(
                    'name' => 'iso_code_3',
                    'id' => 'iso_code_3',
                    'class' => 'st-forminput',
                    'value' => !empty($country->iso_code_3) ? $country->iso_code_3 : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_address_format'],
                'value' => array(
                    'name' => 'address_format',
                    'id' => 'address_format',
                    'class' => 'st-forminput',
                    'value' => !empty($country->address_format) ? $country->address_format : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_postcode_required'],
                'value' => array(
                    'name' => 'postcode_required',
                    'id' => 'postcode_required',
                    'class' => 'st-forminput',
                    'value' => !empty($country->postcode_required) ? $country->postcode_required : ''
                )
            ),
            array(
                'type' => 'dropdown',
                'label' => $this->lang['entry_status'],
                'value' => array(
                    array(
                        'name' => 'status',
                        'options' => array('1' => 'active', '0' => 'de-active'),
                        'selected' => !empty($country->status) ? $country->status : '',
                        'extra' => 'class="uniform"'
                    ),
                )
            )
        );

        $this->data['input_submit'] = array(
            'value' => $this->lang['btn_save'],
            'class' => 'button-blue'
        );
    }

}